/*
 * *****************************************************************************
 *  Copyright ( c ) 2019 huibur Inc. All Rights Reserved.
 *
 *  This software is the confidential and proprietary information of Huibur Inc
 *  (*Confidential Information*). You shall not disclose such Confidential Information
 *  and shall use it only in accordance with the terms of the license agreement
 *  you entered into with Huibur or a Huibur authorized
 *  reseller (the "License Agreement*).
 *  ******************************************************************************
 */
package com.huibur.devicecheck.controller;

import com.huibur.devicecheck.enums.ResultCodeEnum;
import com.huibur.devicecheck.exception.DeviceCategoryNameRepeatException;
import com.huibur.devicecheck.model.param.DeviceCategoryParam;
import com.huibur.devicecheck.model.query.DeviceCategoryQuery;
import com.huibur.devicecheck.model.vo.DeviceCategoryVO;
import com.huibur.devicecheck.request.ObjRes;
import com.huibur.devicecheck.request.PageRes;
import com.huibur.devicecheck.service.IDeviceCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/*******************************************************
* @title: DeviceCategoryController.java
* @description:
* @author wushiqi
* @date 2021/10/18
* @version 1.0
*******************************************************/
@RestController
@RequestMapping("/devicesCategory")
@Api(value = "设备名称管理",tags = {"设备名称管理"})
@Slf4j
public class DeviceCategoryController {

    @Autowired
    IDeviceCategoryService iDeviceCategoryService;

    /**
     * 新增类别
     * @param deviceCategoryParam
     * @return
     */
    @PostMapping("")
    @ApiOperation(value = "新增设备名称",response = ObjRes.class)
    public ObjRes<Void> insertDeviceCategory(@RequestBody DeviceCategoryParam deviceCategoryParam) {
        try{
            return iDeviceCategoryService.insertDeviceCategory(deviceCategoryParam);
        } catch (DeviceCategoryNameRepeatException e) {
            log.error(e.getMessage());
            return new ObjRes(ResultCodeEnum.DEVICE_CATEGORY_NAME_REPEAT.getCode(),ResultCodeEnum.DEVICE_CATEGORY_NAME_REPEAT.getLabel());
        }

    }

    /**
     * 删除设备名称
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除设备名称",response = ObjRes.class)
    public ObjRes<Boolean> deleteById(@PathVariable Long id){
        return new ObjRes<Boolean>().data(iDeviceCategoryService.deleteById(id));
    }

    /**
     * 编辑设备名称
     * @param id
     * @param deviceCategoryParam
     * @return
     */
    @PutMapping("/{id}")
    @ApiOperation(value = "编辑设备名称",response = ObjRes.class)
    public ObjRes<Void> updateDeviceCategory(@PathVariable Long id,@RequestBody DeviceCategoryParam deviceCategoryParam) {
        try{
            return iDeviceCategoryService.updateDeviceCategory(id,deviceCategoryParam);
        } catch (DeviceCategoryNameRepeatException e) {
            log.error(e.getMessage());
            return new ObjRes<Void>(ResultCodeEnum.DEVICE_CATEGORY_NAME_REPEAT.getCode(), ResultCodeEnum.DEVICE_CATEGORY_NAME_REPEAT.getLabel());
        }

    }

    /**
     * 设备名称列表(查询并分页)
     * @param deviceCategoryQuery
     * @return
     */
    @GetMapping("")
    @ApiOperation(value = "设备名称列表(查询并分页)",response = PageRes.class)
    public PageRes<DeviceCategoryVO> selectListDeviceCategory(DeviceCategoryQuery deviceCategoryQuery) {
        return iDeviceCategoryService.selectListDeviceCategory(deviceCategoryQuery);
    }

    /**
     * 获取设备名称详情
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "获取设备名称详情",response = ObjRes.class)
    public ObjRes<DeviceCategoryVO> selectDeviceCategoryById(@PathVariable Long id) {
        return new ObjRes<DeviceCategoryVO>().data(iDeviceCategoryService.selectDeviceCategoryById(id));
    }

}



